/**
 * This project is a database of food additives, denoted code
 * E.
 * (���� ������ ������������ ����� ���� ������ ������� �������,
 * ������������ ����� �)
 * 
 * Copyright (C) 2010  Dmitry Zolotaryov
 *
 * This program is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * http://www.gnu.org/licenses/.
 * 
 * Contact the author:
 * e-mail: zdv2010@gmail.com
 * ICQ: 476-467-654
 * Jabber: c0unt_zer0@jabber.org
 */
package zdv.foodadditives;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;

/**
 * @author Dmitry Zolotaryov
 *
 */
public class MyItemAdapter extends ArrayAdapter<MyItem> {
	int resource;
	Context context;
	
	/**
	 * Constructor
	 * @see android.widget.ArrayAdapter#ArrayAdapter(Context, int, List)
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public MyItemAdapter(Context context, int textViewResourceId,
			List<MyItem> objects) {
		super(context, textViewResourceId, objects);
		resource = textViewResourceId;
		this.context = context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		TableLayout view;
		
		String locale = context.getResources()
			.getConfiguration().locale.getDisplayCountry();
		
		MyItem item = getItem(position);
		String code = item.getCode();
		int categorie = item.getCategorie();
		int danger = item.getDanger();
		String name = null;
		String description = null;
		if(locale.equals("United States")){
			name = item.getName();
			description = item.getDescription();
		} else{
			name = item.getName_ru();
			description = item.getDescription_ru();		
		}

		if(convertView == null){
			view = new TableLayout(getContext());
			LayoutInflater li = (LayoutInflater)getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(resource, view, true);
		} else {
			view = (TableLayout) convertView;
		}
	
		TextView textDanger = (TextView)view.findViewById(R.id.textDanger);		
		switch(danger){
		case MyItem.Danger.SUSPICIOUS:
			view.setBackgroundResource(R.color.suspicious);
			textDanger.setText(R.string.suspicious);
			break;
		case MyItem.Danger.DANGEROUS:
			view.setBackgroundResource(R.color.dangerous);
			textDanger.setText(R.string.dangerous);
			break;
		case MyItem.Danger.VERY_DANGEROUS:
			view.setBackgroundResource(R.color.very_dangerous);
			textDanger.setText(R.string.very_dangerous);
			break;
		}
		
		TextView textECode = (TextView)view.findViewById(R.id.textECode);
		textECode.setText(code);
		
		TextView textCategorie = (TextView)view.findViewById(R.id.textCategorie);
		switch(categorie){
		case MyItem.Categories.COLORANT:
			textCategorie.setText(R.string.colorant);
			break;
		case MyItem.Categories.PRESERVATIVE:
			textCategorie.setText(R.string.preservative);
			break;
		case MyItem.Categories.ANTIOXIDANT:
			textCategorie.setText(R.string.antioxidant);
			break;
		case MyItem.Categories.STABILIZER:
			textCategorie.setText(R.string.stabilizer);
			break;
		case MyItem.Categories.REGULATOR:
			textCategorie.setText(R.string.regulator);
			break;
		case MyItem.Categories.FLAVOR:
			textCategorie.setText(R.string.flavor);
			break;
		case MyItem.Categories.ANTIBIOTIC:
			textCategorie.setText(R.string.antibiotic);
			break;
		case MyItem.Categories.ANTIFOAM_AGENT:
			textCategorie.setText(R.string.antifoam_agent);
			break;
		}
		
		TextView textName = (TextView)view.findViewById(R.id.textName);
		textName.setText(name);
		
		TextView textDescription = (TextView)view.findViewById(R.id.textDescription);
		textDescription.setText(description);
		
		return view;
	}
}
